﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KingSite.Shop.Core.Service;
using KingSite.Shop.Domain.ShowDataModel;
using KingSite.Shop.Domain;

namespace KingSite.Shop.Service {
    public class ShoppingCart : IShoppingCart {

        private ICartService _cartService;
        private ICartDetailService _cartDetailService;
        private int _ShoppingCartId;
        private int _CustomerId;
        private ShoppingCartItems _CartItems;
        IShippingFee _shippingFee;

        public ShoppingCart(int shoppingCartId, int customerId)
            : this(shoppingCartId, customerId, new ShippingFee()) {

        }

        private ShoppingCart(int shoppingCartId, int customerId, IShippingFee fee) {
            this._CustomerId = customerId;
            this._ShoppingCartId = shoppingCartId;
            this._shippingFee = fee;

        }

        #region IShoppingCart 成员
        public int ShoppingCartId {
            get { return _ShoppingCartId; }
        }

        public int CustomerId {
            get { return _CustomerId; }
        }


        public int ItemCount {
            get { return CartItems.Count; }
        }

        public decimal TotalValue {
            get {
                decimal total = 0;
                foreach (CartItem item in _CartItems.Values) {
                    total += item.ItemSubTotal;
                }
                return total;
            }
        }

        public decimal RealValue {
            get {
                return TotalValue;
            }
        }

        public ShoppingCartItems CartItems {
            get {
                if (_CartItems == null) {
                    CartItems = _cartService.GetCartItems(this.ShoppingCartId);
                    CartItems = CartItems.Sort();
                }
                return _CartItems;
            }
            set {
                _CartItems = value;
            }
        }

        public decimal ShippingFee {
            get { return this._shippingFee.Fee; }
        }


        #endregion

        #region ShoppingCart Method
        public int Add(int skuId) {
            return Add(skuId, 1);
        }

        public int Add(int skuId, int quantity) {
            CartDetail cartDetail = _cartDetailService.GetCartDetailByCartId_SkuId_LinkType(this._ShoppingCartId, skuId, "");
            int ItemId;
            if (cartDetail == null) {
                ItemId = _cartDetailService.Insert(ShoppingCartId, skuId, quantity, out cartDetail);
            }
            else {
                ItemId = cartDetail.Id;
                SetQuantity(ItemId, cartDetail.Quantity + quantity);
            }
            return ItemId;
        }

        public void Remove(int id) {
            _cartDetailService.Delete(id);
        }

        public void SetQuantity(int id, int quantity) {
            _cartDetailService.UpdateCartDetailQuantity(id, quantity);
        }

        public void Clear() {
            _CartItems.Values.Clear();
        }
        #endregion
    }
}
